package id.co.shuba.spamobile.widget;

import java.text.NumberFormat;

import id.co.shuba.spamobile.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Typeface;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class PipelineCursorAdapter extends CursorAdapter {
	private Context mContext;
	private NumberFormat nf;

	public class ViewHolder {
		LinearLayout panelItem;
		TextView txtName;
		TextView txtAddress;
		TextView txtPlafon;		
	}

	public PipelineCursorAdapter(Context context, Cursor c, int flags) {
		super(context, c, flags);
		mContext = context;
		nf = NumberFormat.getInstance();		
	}

	public PipelineCursorAdapter(Context context, Cursor c, boolean autoRequery) {
		super(context, c, autoRequery);
		mContext = context;
		nf = NumberFormat.getInstance();
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		View view = LayoutInflater.from(mContext)
				.inflate(R.layout.pipeline_item , parent, false);
		ViewHolder viewHolder = new ViewHolder();
		viewHolder.panelItem = (LinearLayout) view.findViewById(R.id.panelItem);
		viewHolder.txtName = (TextView) view.findViewById(R.id.txtName);
		viewHolder.txtAddress = (TextView) view.findViewById(R.id.txtAddress);
		viewHolder.txtPlafon = (TextView) view.findViewById(R.id.txtPlafon);
		view.setTag(viewHolder);
		return view;
	}
	
	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		boolean inbox = cursor.getInt(cursor.getColumnIndex("INBOX")) == 1;
		
		ViewHolder viewHolder = (ViewHolder) view.getTag();		
		viewHolder.txtName.setText(cursor.getString(cursor.getColumnIndex("FULL_NAME")));
		viewHolder.txtAddress.setText(cursor.getString(cursor.getColumnIndex("ADDRESS_BUSINESS")));
		viewHolder.txtPlafon.setText(nf.format(cursor.getLong(cursor.getColumnIndex("RENCANA_PLAFON"))));
		
		if (inbox) {
			//viewHolder.panelItem.setBackgroundColor(mContext.getResources().getColor(android.R.color.holo_orange_light));
			viewHolder.txtName.setTypeface(null, Typeface.BOLD_ITALIC);
			//viewHolder.txtAddress.setTypeface(null, Typeface.BOLD);
		} else {
			//viewHolder.panelItem.setBackgroundColor(mContext.getResources().getColor(android.R.color.background_light));
			viewHolder.txtName.setTypeface(null, Typeface.NORMAL);
			//viewHolder.txtAddress.setTypeface(null, Typeface.NORMAL);
		}
	}
}
